using System;
using System.Collections.Generic;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestDMPropertySelectorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        //[Ignore("Changing how this works")] 
        [Test]
        public void Test_Construction()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.AreEqual(2, control.Controls.Count);
            Assert.IsInstanceOf(typeof (IGridControl), control.Controls[0]);
            IGridControl gridControl = (IGridControl) control.Controls[0];
            IButtonGroupControl buttonsControl = gridControl.Buttons;
            Assert.IsTrue(buttonsControl.Visible);
            Assert.IsTrue(gridControl.FilterControl.Visible);
            Assert.IsNull(buttonsControl["New"]);
            Assert.IsNotNull(buttonsControl["Cancel"]);
            Assert.IsNull(buttonsControl["Remove"]);
            Assert.IsNotNull(GetPKButton(control));
            Assert.IsNotNull(buttonsControl["Save"]);
            Assert.IsFalse(buttonsControl["Save"].Visible);
//            Assert.IsNotNull(buttonsControl["New"]);
//            Assert.IsNotNull(buttonsControl["Cancel"]);
//            Assert.IsNotNull(buttonsControl["Remove"]);
//            Assert.IsFalse(buttonsControl["Add"].Visible);
//            Assert.IsFalse(buttonsControl["Edit"].Visible);
//            Assert.IsFalse(buttonsControl["Delete"].Visible);
        }

        [Test]
        public void TestSetDMClass()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreEqual(dmClass.Properties.Count, control.BusinessObjectCollection.Count);
            Assert.AreEqual(dmClass.Properties.Count, control.Properties.Count);
        }

        [Test]
        public void TestSetDMClass_Null_ClearsCollectionAndSelectedProperty()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreEqual(dmClass.Properties.Count, control.BusinessObjectCollection.Count);
            Assert.AreEqual(dmClass.Properties.Count, control.Properties.Count);
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsNull(control.Class);
            Assert.IsNull(control.SelectedProperty);
            Assert.IsNull(control.BusinessObjectCollection);
        }


        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
            Assert.IsFalse(GetPKButton(control).Enabled, "PK should be disabled at creation");
        }

        [Test]
        public void Test_DmClass_HasOneProperty_ShouldEnableSelectorAndPKButton()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            dmClass.Properties.CreateBusinessObject();
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            //---------------Assert Preconditions---------------
            Assert.IsFalse(GetPKButton(control).Enabled);
            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
            Assert.IsTrue(GetPKButton(control).Enabled);
        }

        [Test]
        public void Test_DmClass_HasZeroProperties_ShouldEnableSelectorAndDisablePKButton()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateUnsavedValidDMClass();
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            //---------------Assert Preconditions---------------
            Assert.IsFalse(GetPKButton(control).Enabled);
            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
            Assert.IsFalse(GetPKButton(control).Enabled);
        }

        [Test]
        public void TestSetToNullDisables()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
            Assert.IsFalse(GetPKButton(control).Enabled);
        }

        [Test]
        public void TestSetBusinessObject()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.SelectedProperty = dmProperty;
            //---------------Test Result -----------------------
            Assert.AreSame(dmProperty, control.SelectedProperty);
            Assert.AreSame(control.SelectedProperty, control.GridControl.SelectedBusinessObject);
            Assert.AreSame(control.SelectedProperty, control.SelectedBusinessObject);
        }

        [Test]
        public void TestBusinessObjectSelectedEvent()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            bool fired = false;
            DMProperty selected = null;
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            control.BusinessObjectSelected += (sender, e) =>
                                        {
                                            fired = true;
                                            selected = (DMProperty) e.BusinessObject;
                                        };
            control.SelectedProperty = dmProperty;
            //---------------Test Result -----------------------

            Assert.IsTrue(fired);
            Assert.AreSame(selected, control.SelectedProperty);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_TogglePK_FromNull()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            TestUtilsShared.CreateUnsavedValidDMProperty(bo);
            DMProperty propertyInfo2 = TestUtilsShared.CreateUnsavedValidDMProperty(bo);
            TestUtilsShared.CreateUnsavedValidDMProperty(bo);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = bo;
            //-------------Assert Preconditions -------------
            Assert.IsNull(bo.ObjectIdentity);
            //---------------Execute Test ----------------------
            control.SelectedProperty = propertyInfo2;
            control.TogglePK();
            //---------------Test Result -----------------------
            Assert.IsNotNull(bo.ObjectIdentity);
            DMObjectIdentity dmObjectIdentity = bo.ObjectIdentity;
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.AreSame(propertyInfo2, dmObjectIdentity.Properties[0]);
            Assert.IsNotNull(propertyInfo2.ObjectIdentity);
            Assert.IsTrue(propertyInfo2.IsPartOfObjectIdentity);
        }

        [Test]
        public void Test_TogglePK_WithTheSameSelectedItems_RemovesThemFromPrimaryKey()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMProperty(bo);
            DMProperty propertyInfo2 = TestUtilsShared.CreateSavedDMProperty(bo);
            TestUtilsShared.CreateSavedDMProperty(bo);
            TestUtilsShared.CreateSavedDMProperty(bo);
            TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = bo;
            control.SelectedProperty = propertyInfo2;
            control.TogglePK();
            //-------------Assert Preconditions -------------
            Assert.IsNotNull(bo.ObjectIdentity);
            DMObjectIdentity dmObjectIdentity = bo.ObjectIdentity;
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.AreSame(propertyInfo2, dmObjectIdentity.Properties[0]);
            Assert.IsNotNull(propertyInfo2.ObjectIdentity);
            Assert.IsTrue(propertyInfo2.IsPartOfObjectIdentity);
            //---------------Execute Test ----------------------
            control.TogglePK();
            //---------------Test Result -----------------------
            //Assert.AreEqual(0, bo.ObjectIdentity.Properties.Count);
            dmObjectIdentity = bo.ObjectIdentity;
            Assert.IsNotNull(dmObjectIdentity);
            Assert.AreEqual(0, dmObjectIdentity.Properties.Count);
        }

        [Test]
        public void Test_TogglePK_Reset()
        {
            //---------------Set up test pack-------------------
            DMProperty propertyInfo2;
            DMProperty propertyInfo3;
            DMClass bo = CreateDMClassWith5Properties(out propertyInfo2, out propertyInfo3);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin())
                                                     {Class = bo, SelectedProperty = propertyInfo2};
            control.TogglePK();
            //-------------Assert Preconditions -------------
            Assert.IsNotNull(bo.ObjectIdentity);
            DMObjectIdentity dmObjectIdentity = bo.ObjectIdentity;
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.AreSame(propertyInfo2, dmObjectIdentity.Properties[0]);
            Assert.IsNotNull(propertyInfo2.ObjectIdentity);
            Assert.IsTrue(propertyInfo2.IsPartOfObjectIdentity);
            //---------------Execute Test ----------------------
            control.SelectedProperty = propertyInfo3;
            control.TogglePK();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.GridControl.Grid.SelectedBusinessObjects.Count);
            Assert.IsNotNull(bo.ObjectIdentity);
            dmObjectIdentity = bo.ObjectIdentity;
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.IsTrue(dmObjectIdentity.Properties.Contains(propertyInfo3));
            Assert.IsNull(propertyInfo2.ObjectIdentity);
            Assert.IsFalse(propertyInfo2.IsPartOfObjectIdentity);
            Assert.IsNotNull(propertyInfo3.ObjectIdentity);
            Assert.IsTrue(propertyInfo3.IsPartOfObjectIdentity);
        }

        private static DMClass CreateDMClassWith5Properties(out DMProperty propertyInfo2, out DMProperty propertyInfo3)
        {
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            TestUtilsShared.UpdateDMPropertyWithValidData(bo.CreateDMProp());
            propertyInfo2 = bo.CreateDMProp();
            propertyInfo3 = bo.CreateDMProp();
            TestUtilsShared.UpdateDMPropertyWithValidData(propertyInfo2);
            TestUtilsShared.UpdateDMPropertyWithValidData(propertyInfo3);

            TestUtilsShared.UpdateDMPropertyWithValidData(bo.CreateDMProp());
            TestUtilsShared.UpdateDMPropertyWithValidData(bo.CreateDMProp());
            return bo;
        }

        [Test]
        public void Test_ClickTogglePKButton_FromNull()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateUnsavedValidDMClass();
            TestUtilsShared.CreateUnsavedValidDMProperty(bo);
            DMProperty propertyInfo2 = TestUtilsShared.CreateUnsavedValidDMProperty(bo);
            TestUtilsShared.CreateUnsavedValidDMProperty(bo);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = bo;
            //-------------Assert Preconditions -------------
            Assert.IsNull(bo.ObjectIdentity);
            //---------------Execute Test ----------------------
            control.SelectedProperty = propertyInfo2;
            GetPKButton(control).PerformClick();
            //---------------Test Result -----------------------
            Assert.IsNotNull(bo.ObjectIdentity);
            DMObjectIdentity dmObjectIdentity = bo.ObjectIdentity;
            Assert.AreEqual(1, dmObjectIdentity.Properties.Count);
            Assert.AreSame(propertyInfo2, dmObjectIdentity.Properties[0]);
            Assert.IsNotNull(propertyInfo2.ObjectIdentity);
            Assert.IsTrue(propertyInfo2.IsPartOfObjectIdentity);
        }

        //ToDetermine whether we will have a new button or not.
//        [Ignore("Changing how this works")] 
//        [Test]
//        public void TestNewButtonClickedCreatesBO_ExistingCollection()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            DMPropertySelectorControl classSelectorControl =
//                new DMPropertySelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = classSelectorControl.GridControl;
//            classSelectorControl.Class = dmClass;
//            //  ---------------Assert Precondition----------------
//            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
//            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
//            Assert.AreEqual(1, classes.Count);
//            //  ---------------Execute Test ----------------------
//            gridControl.Buttons["New"].PerformClick();
//            // ---------------Test Result -----------------------
//            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(2, classes.Count);
//            Assert.AreSame(classes[1], gridControl.SelectedBusinessObject);
//        }
//        [Ignore("Changing how this works")] 
//        [Test]
//        public void TestDeleteButtonDeletesCurrentBO()
//        {
//            // ---------------Set up test pack-------------------
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
//            DMPropertySelectorControl classSelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = classSelectorControl.GridControl;
//            classSelectorControl.Class = dmClass;
//            IBusinessObject currentBO = gridControl.SelectedBusinessObject;
//            //  ---------------Assert Precondition----------------
//            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(1, classes.Count);
//            //  ---------------Execute Test ----------------------
//            gridControl.Buttons["Remove"].PerformClick();
//            // ---------------Test Result -----------------------
//            Assert.IsTrue(currentBO.Status.IsDeleted);
//            Assert.IsFalse(currentBO.Status.IsDirty);
//            Assert.AreEqual(0, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(0, classes.Count);
//        }

//        [Ignore("Changing how this works")] 
//        [Test]
//        public void TestDeleteButton_NoCurrentBO()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
//            DMPropertySelectorControl classSelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = classSelectorControl.GridControl;
//            classSelectorControl.Class = dmClass;
//            IBusinessObject currentBO = gridControl.SelectedBusinessObject;
//            //  ---------------Assert Precondition----------------
//            Assert.AreEqual(0, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(0, classes.Count);
//            Assert.IsNull(currentBO);
//            //  ---------------Execute Test ----------------------
//            gridControl.Buttons["Remove"].PerformClick();
//            // ---------------Test Result -----------------------
//            Assert.AreEqual(0, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(0, classes.Count);
//        }

        //[Ignore("Changing how this works")] 
        [Test]
        public void TestCancelButton_ClickRestoresSavedObject()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMProperty(dmClass);
            BusinessObjectCollection<DMProperty> dmProperties = dmClass.Properties;
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            IGridControl gridControl = propertySelectorControl.GridControl;
            propertySelectorControl.Class = dmClass;
            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
            currentBO.DisplayName = TestUtilsShared.GetRandomString();
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
            Assert.AreEqual(1, dmProperties.Count);
            Assert.IsNotNull(currentBO);
            Assert.IsTrue(currentBO.Status.IsDirty);
            //  ---------------Execute Test ----------------------
            gridControl.Buttons["Cancel"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.IsFalse(currentBO.Status.IsDirty);
        }

        [Test]
        public void TestCancelButton_Click_NoObjecctSelected()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateUnsavedValidDMClass();
            BusinessObjectCollection<DMProperty> dmProperties = dmClass.Properties;
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            IGridControl gridControl = propertySelectorControl.GridControl;
            propertySelectorControl.Class = dmClass;
            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
            //  ---------------Assert Precondition----------------
            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
            Assert.AreEqual(0, dmProperties.Count);
            Assert.IsNull(currentBO);
            //  ---------------Execute Test ----------------------
            gridControl.Buttons["Cancel"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
            Assert.AreEqual(0, dmProperties.Count);
        }

        //[Ignore("Changing how this works")] 
//        [Test]
//        public void TestCancelButton_ClickRemovesNewObject_OnlyItemInGrid()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
//            DMPropertySelectorControl classSelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = classSelectorControl.GridControl;
//            classSelectorControl.Class = dmClass;
//            IButton cancelButton = gridControl.Buttons["Cancel"];
//            IButton newButton = gridControl.Buttons["New"];
//
//            newButton.PerformClick();
//            //---------------Assert Precondition----------------
//            Assert.IsTrue(cancelButton.Enabled);
//            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
//            //---------------Execute Test ----------------------
//            cancelButton.PerformClick();
//            //---------------Test Result -----------------------
//            Assert.AreEqual(1, classes.Count, "The new item should be removed from the collection");
//            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
//        }/

        //[Ignore("Changing how this works")] 
//        [Test]
//        public void TestCancelButton_ClickRemovesNewObject_TwoItemsInGrid()
//        {
//            // ---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            DMPropertySelectorControl classSelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = classSelectorControl.GridControl;
//            classSelectorControl.Class = dmClass;
//            IButton cancelButton = gridControl.Buttons["Cancel"];
//            IButton newButton = gridControl.Buttons["New"];
//            newButton.PerformClick();
//            // ---------------Assert Precondition----------------
//            Assert.AreEqual(3, gridControl.Grid.Rows.Count);
//            //   ---------------Execute Test ----------------------
//            cancelButton.PerformClick();
//            //  ---------------Test Result -----------------------
//            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
//            Assert.IsNotNull(gridControl.SelectedBusinessObject);
//        }

        //[Ignore("Changing how this works")] 
        //Need to figure out how to put a new item into datasetProvider/Grid
//        [Test]
//        public void Test_ObjectSavesWhenSaveButtonClicked()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            DMPropertySelectorControl classSelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = classSelectorControl.GridControl;
//            classSelectorControl.Class = dmClass;
//            gridControl.Buttons["New"].PerformClick();
//            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
//            TestUtilsShared.UpdateDMPropertyWithValidData(currentBO);
//            currentBO.DisplayName = TestUtilsShared.GetRandomString();
//
//            //---------------Assert Precondition----------------
//            Assert.IsNotNull(currentBO);
//            Assert.IsTrue(currentBO.Status.IsNew);
//            Assert.IsTrue(currentBO.Status.IsDirty);
//            Assert.IsTrue(currentBO.IsValid());
//            //  ---------------Execute Test ----------------------
//            gridControl.Buttons["Save"].PerformClick();
//            // ---------------Test Result -----------------------
//            Assert.IsFalse(currentBO.Status.IsDirty);
//            Assert.IsFalse(currentBO.Status.IsNew);
//            Assert.IsFalse(currentBO.Status.IsDeleted);
//        }

        //[Ignore("Changing how this works")] 
        [Test]
        public void Test_SaveButtonClicked_NoObjectSelected_DoesNotRaiseError()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateUnsavedValidDMClass();
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            IGridControl gridControl = propertySelectorControl.GridControl;
            propertySelectorControl.Class = dmClass;
            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
            //---------------Assert Precondition----------------
            Assert.IsNull(currentBO);
            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
            //  ---------------Execute Test ----------------------
            gridControl.Buttons["Save"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
        }

        [Test]
        public void Test_HasMoveOrdinalPositionsButtons()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());

            //---------------Test Result -----------------------
            Assert.AreEqual(2, propertySelectorControl.Controls.Count);
            Assert.IsNotNull(propertySelectorControl.MovePositionPanel);
            IPanel ordinalPositionPanel = propertySelectorControl.MovePositionPanel;
            Assert.Greater(ordinalPositionPanel.Left, propertySelectorControl.GridControl.Left);
            Assert.AreEqual(3, ordinalPositionPanel.Controls.Count);
            Assert.IsInstanceOf(typeof(IPanel), ordinalPositionPanel.Controls[0]);
            Assert.AreEqual("", ordinalPositionPanel.Controls[1].Text);
            Assert.AreEqual("", ordinalPositionPanel.Controls[2].Text);

        }

        [Test]
        public void Test_ClickMoveUp_MovesItemUp()
        {
            //---------------Set up test pack-------------------
            //---------------Set up test pack-------------------
            DMProperty propertyInfo2;
            DMProperty propertyInfo3;
            DMClass bo = CreateDMClassWith5Properties(out propertyInfo2, out propertyInfo3);
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin()) { Class = bo, SelectedProperty = propertyInfo2 };
            //---------------Assert Precondition----------------
            Assert.IsNotNull(propertySelectorControl.Class);
            Assert.IsNotNull(propertySelectorControl.SelectedBusinessObject);
            Assert.AreEqual(2, propertyInfo2.Order);
            Assert.AreSame(propertyInfo2, propertySelectorControl.GridControl.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            GetMoveUpButton(propertySelectorControl).PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, propertyInfo2.Order);
        }

        [Test]
        public void Test_ClickMoveUp_MovesItemUp_SavedProps()
        {
            //---------------Set up test pack-------------------
            //---------------Set up test pack-------------------
            DMProperty propertyInfo2;
            DMProperty propertyInfo3;
            DMClass bo = CreateDMClassWith5Properties(out propertyInfo2, out propertyInfo3);
            bo.Save();
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin()) { Class = bo, SelectedProperty = propertyInfo2 };
            //---------------Assert Precondition----------------
            Assert.IsNotNull(propertySelectorControl.Class);
            Assert.IsNotNull(propertySelectorControl.SelectedBusinessObject);
            Assert.AreEqual(2, propertyInfo2.Order);
            Assert.AreSame(propertyInfo2, propertySelectorControl.GridControl.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            GetMoveUpButton(propertySelectorControl).PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, propertyInfo2.Order);
        }
        [Test]
        public void Test_ClickMoveDown_MovesItemDown()
        {
            //---------------Set up test pack-------------------
            //---------------Set up test pack-------------------
            DMProperty propertyInfo2;
            DMProperty propertyInfo3;
            DMClass bo = CreateDMClassWith5Properties(out propertyInfo2, out propertyInfo3);
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin()) { Class = bo, SelectedProperty = propertyInfo2 };
            //---------------Assert Precondition----------------
            Assert.IsNotNull(propertySelectorControl.Class);
            Assert.IsNotNull(propertySelectorControl.SelectedBusinessObject);
            Assert.AreEqual(2, propertyInfo2.Order);
            //---------------Execute Test ----------------------
            GetMoveDownButton(propertySelectorControl).PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual(3, propertyInfo2.Order);
        }

        [Test]
        public void Test_ClickMoveDown_NoClassDoesNotGiveError()
        {
            //---------------Set up test pack-------------------
            //---------------Set up test pack-------------------
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin()) ;
            //---------------Assert Precondition----------------
            Assert.IsNull(propertySelectorControl.Class);
            //---------------Execute Test ----------------------
            GetMoveDownButton(propertySelectorControl).PerformClick();
            //---------------Test Result -----------------------
            Assert.IsNull(propertySelectorControl.Class, "Should not have raised error");
        }

        [Test]
        public void Test_ClickMoveUp_NoClassDoesNotGiveError()
        {
            //---------------Set up test pack-------------------
            //---------------Set up test pack-------------------
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin()) ;
            //---------------Assert Precondition----------------
            Assert.IsNull(propertySelectorControl.Class);
            //---------------Execute Test ----------------------
            GetMoveUpButton(propertySelectorControl).PerformClick();
            //---------------Test Result -----------------------
            Assert.IsNull(propertySelectorControl.Class, "Should not have raised error");
        }

        [Test]
        public void Test_UniqueConstraintButton()
        {
            //--------------- Set up test pack ------------------
            //--------------- Test Preconditions ----------------
            //--------------- Execute Test ----------------------
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            //--------------- Test Result -----------------------
            IButtonGroupControl buttonGroupControl = propertySelectorControl.GridControl.Buttons;
            IButton button = buttonGroupControl["UC"];
            Assert.IsNotNull(button);
            Assert.IsFalse(button.Enabled);
        }

        [Test]
        public void Test_UniqueConstraintButtonDisabledNoProps()
        {
            //--------------- Set up test pack ------------------
            DMClass dmClass = TestUtilsShared.CreateUnsavedValidDMClass();
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            //--------------- Test Preconditions ----------------
            Assert.AreEqual(0, dmClass.Properties.Count);
            //--------------- Execute Test ----------------------
            propertySelectorControl.Class = dmClass;
            //--------------- Test Result -----------------------
            IButtonGroupControl buttonGroupControl = propertySelectorControl.GridControl.Buttons;
            IButton button = buttonGroupControl["UC"];
            Assert.IsNotNull(button);
            Assert.IsFalse(button.Enabled);
        }

        [Test]
        public void Test_PKButton_ShouldBeDisabled_WhenClassGenerated()
        {
            //--------------- Set up test pack ------------------
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            IDMClass dmClass = dmProperty.Class;
            dmClass.Generated = true;
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            propertySelectorControl.Class = (DMClass) dmClass;
            //--------------- Test Preconditions ----------------
            Assert.IsTrue(dmClass.Generated.GetValueOrDefault());
            //--------------- Execute Test ----------------------
            propertySelectorControl.GridControl.SelectedBusinessObject = dmProperty;
            //--------------- Test Result -----------------------
            Assert.IsFalse(GetPKButton(propertySelectorControl).Enabled);
        }

        [Test]
        public void Test_TogglePK_ShouldRaiseErrorIfClassGenerated()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateUnsavedValidDMClass();
            dmClass.Generated = true;
            TestUtilsShared.CreateUnsavedValidDMProperty(dmClass);
            DMProperty propertyInfo2 = TestUtilsShared.CreateUnsavedValidDMProperty(dmClass);
            propertyInfo2.Generated = false;
            TestUtilsShared.CreateUnsavedValidDMProperty(dmClass);
            DMPropertySelectorControl control = new DMPropertySelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //-------------Assert Preconditions -------------
            Assert.IsNull(dmClass.ObjectIdentity);
            Assert.IsTrue(dmClass.Generated.Value);
            //---------------Execute Test ----------------------
            control.SelectedProperty = propertyInfo2;
            try
            {
                control.TogglePK();
                Assert.Fail("expected Err");
            }
            //---------------Test Result -----------------------
            catch (Exception ex)
            {
                StringAssert.Contains("The primary key cannot be changed as the entity '" + dmClass.ToString() + "' has already been exported.", ex.Message);
            }
        }

        //[Test]
        //public void Test_UniqueConstraintButtonDisabled_NoSelectedProperty()
        //{
        //    //--------------- Set up test pack ------------------
        //    DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
        //    //--------------- Test Preconditions ----------------
        //    Assert.AreEqual(0, dmClass.Properties.Count);
        //    //--------------- Execute Test ----------------------
        //    DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
        //    //--------------- Test Result -----------------------
            
        //    Assert.IsNotNull(button);
        //    Assert.IsFalse(button.Enabled);
        //}


        [Test]
        public void Test_UniqueConstraintButtonClick()
        {
            //--------------- Set up test pack ------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            TestUtilsShared.CreateSavedDMProperty(dmClass);
            DMProperty propertyInfo2 = TestUtilsShared.CreateSavedDMProperty(dmClass);
            DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
            propertySelectorControl.Class = dmClass;
            IButtonGroupControl buttonGroupControl = propertySelectorControl.GridControl.Buttons;
            IButton ucButton = buttonGroupControl["UC"];
            //--------------- Test Preconditions ----------------
            Assert.AreEqual(0, dmClass.UniqueConstraints.Count);
            //--------------- Execute Test ----------------------
            propertySelectorControl.GridControl.SelectedBusinessObject = propertyInfo2;
            ucButton.PerformClick();
            //--------------- Test Result -----------------------
            BusinessObjectCollection<DMUniqueConstraint> uniqueConstraints = dmClass.UniqueConstraints;
            Assert.AreEqual(1, uniqueConstraints.Count);
            DMUniqueConstraint uniqueConstraint = uniqueConstraints[0];
            BusinessObjectCollection<DMUniqueConstraintProperty> uniqueConstraintProperties = uniqueConstraint.UniqueConstraintProperties;
            Assert.AreEqual(1, uniqueConstraintProperties.Count);
            Assert.AreEqual(propertyInfo2.PropertyName, uniqueConstraint.UniqueConstraintName);
        }

        //[Test]
        //public void Test_MoveUpClick_ClearsFilter()
        //{
        //    //--------------- Set up test pack ------------------
        //    DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
        //    DMProperty dmProperty1 = dmClass.Properties.CreateBusinessObject();
        //    dmProperty1.PropertyName = "aaaaa";
        //    DMProperty dmProperty2 = dmClass.Properties.CreateBusinessObject();
        //    dmProperty2.PropertyName = "ddddd";
        //    DMPropertySelectorControl propertySelectorControl = new DMPropertySelectorControl(new ControlFactoryWin());
        //    IFormHabanero form = GlobalUIRegistry.ControlFactory.CreateForm();
        //     form.Controls.Add(propertySelectorControl); 
        //    propertySelectorControl.Class = dmClass;
        //    IGridControl gridControl = propertySelectorControl.GridControl;
        //    IControlHabanero filterTextBox = gridControl.FilterControl.GetChildControl("PropertyName");
        //    filterTextBox.Text = "aaa";
        //    gridControl.FilterControl.ApplyFilter();
        //    //--------------- Test Preconditions ----------------
        //    Assert.AreEqual(2, dmClass.Properties.Count);
        //    Assert.IsFalse(string.IsNullOrEmpty(filterTextBox.Text));
        //    //Assert.AreEqual(1, gridControl.Grid.Rows.Count);
        //    //--------------- Execute Test ----------------------
        //    GetMoveUpButton(propertySelectorControl).PerformClick();
        //    //--------------- Test Result -----------------------
        //    filterTextBox = gridControl.FilterControl.GetChildControl("PropertyName");
        //    Assert.AreEqual("", filterTextBox.Text);
        //    Assert.AreEqual(2, gridControl.Grid.Rows.Count);
        //}

        private static IButton GetMoveUpButton(DMPropertySelectorControl propertySelectorControl)
        {
            return ((IButton)propertySelectorControl.Controls[1].Controls[1]);
        }
        private static IButton GetMoveDownButton(DMPropertySelectorControl propertySelectorControl)
        {
            return ((IButton)propertySelectorControl.Controls[1].Controls[2]);
        }

        private static IButton GetPKButton(DMPropertySelectorControl control)
        {
            return control.PKButton;
        }

    }
}